Nginx Load Balancing: Simple Configuration for Multi-Server Traffic Distribution
This article introduces Nginx load balancing configuration to solve the problem of excessive load on a single server. At least two backend servers running the same service are required, with Nginx installed and the backend ports open. The core configuration consists of two steps: first, define the backend server group using `upstream` (supporting round-robin, weight, and health checks, e.g., `server 192.168.1.100:8080 weight=2;` or `max_fails=2 fail_timeout=10s`); second, configure `proxy_pass` to this group in the `server` block, passing the client's `Host` and real IP (`proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;`). Verification involves running `nginx -t` to check syntax, `nginx -s reload` to restart, and testing access to confirm request distribution. Common issues such as unresponsive backends or configuration errors can be resolved by checking firewalls and logs. Advanced strategies include IP hashing (`ip_hash`) and URL hashing (requires additional module).
Read More